package 数组;

/**
 * @Author Weton Li
 * @Date 2021/4/16 19:53
 */
public class 最大子序和 {


    public static int method(int[] nums){
        int curr_sum = 0,max_sum = nums[0];
        for (int i = 1; i < nums.length; i++) {

            curr_sum = curr_sum+nums[i];
            curr_sum = Math.max(curr_sum,nums[i]); // 之前和与当前值相比较
            max_sum = Math.max(max_sum,curr_sum); // 最大和和当前和相比较

        }
            return max_sum;
    }

    public static void main(String[] args) {
//        int[] input = {-3,4,-1,2,1};
        int[] input1 = {-2,1,-3,4,-1,2,1,-5,4};
        System.out.println(method(input1));
    }
}
